import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/style/colors.dart';

///限制最大输入edittext，支持单行及多行
class MaxInputEditTextPage extends StatefulWidget {
  final String hintText;
  final int maxLength;
  final int showLine;
  final String content;
  final void Function(String)? onChanged;

  MaxInputEditTextPage({
    Key? key,
    required this.maxLength,
    this.content = "",
    this.hintText = "",
    this.showLine = 1,
    this.onChanged,
  }) : super(key: key);

  ///传入maxCount 则会限制 最大图片展示数量

  @override
  MaxInputEditTextPageState createState() => MaxInputEditTextPageState(
      hintText, content, maxLength, showLine, onChanged);
}

class MaxInputEditTextPageState extends State<MaxInputEditTextPage> {
  String text;
  String hintText;
  int maxLength;
  int showLine;

  final void Function(String)? _onChanged;

  final _textEditingController = TextEditingController();

  MaxInputEditTextPageState(
      this.hintText, this.text, this.maxLength, this.showLine, this._onChanged);

  @override
  void initState() {
    _textEditingController.text = text;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
          border: Border.all(color: ThemeColors.grayE5E5E5, width: 1)),
      child: Stack(
        children: <Widget>[_myTextField(), _getTextCountWidth()],
      ),
    );
  }

  _getTextCountWidth() {
    return Positioned(
      bottom: showLine == 1 ? 13 : 9,
      right: 11,
      child: Text(
        "${text.runes.length}/$maxLength",
        style: TextStyle(color: ThemeColors.text888, fontSize: 12),
      ),
    );
  }

  Widget _myTextField() {
    return Container(
      padding: EdgeInsets.only(
          right: showLine == 1 ? 55 : 0, bottom: showLine == 1 ? 0 : 24),
      margin: EdgeInsets.only(
          left: 10, right: 10, bottom: showLine == 1 ? 13 : 9, top: 10),
      child: TextField(
        maxLength: maxLength,
        maxLengthEnforcement: MaxLengthEnforcement.truncateAfterCompositionEnds,
        maxLines: showLine,
        controller: _textEditingController,
        style: TextStyle(
          color: ThemeColors.text333,
          fontSize: 12.0,
          fontWeight: FontWeight.w500,
        ),
        decoration: InputDecoration(
          fillColor: Colors.white,
          counterText: '',
          isCollapsed: true,
          filled: true,
          hintStyle: TextStyle(
            color: ThemeColors.text888,
            fontSize: 12,
            fontWeight: FontWeight.w500,
          ),
          hintText: hintText,
          //不需要输入框下划线
          border: InputBorder.none,
          suffixIconConstraints: BoxConstraints(maxWidth: 80),
        ),
        //obscureText: true,
        onChanged: (value) {
          if (_onChanged != null) {
            _onChanged!(value);
          }
          setState(() {
            text = value;
          });
        },
      ),
    );
  }
}
